package com.hqd.ch03.v50.boot.context.config;

import com.hqd.ch03.v50.io.FileUrlResource;
import com.hqd.ch03.v50.io.Resource;
import com.hqd.ch03.v50.io.support.FileSystemResource;

import java.io.IOException;

/**
 * 标准配置文件资源
 */
public class StandardConfigDataResource extends ConfigDataResource {

    /**
     * 配置文件信息
     */
    private final StandardConfigDataReference reference;
    /**
     * 资源信息
     */
    private final Resource resource;

    private final boolean emptyDirectory;

    public StandardConfigDataResource(StandardConfigDataReference reference, Resource resource) {
        this(reference, resource, false);
    }

    StandardConfigDataResource(StandardConfigDataReference reference, Resource resource, boolean emptyDirectory) {
        this.reference = reference;
        this.resource = resource;
        this.emptyDirectory = emptyDirectory;
    }

    public StandardConfigDataReference getReference() {
        return this.reference;
    }

    public Resource getResource() {
        return this.resource;
    }

    public String getProfile() {
        return this.reference.getProfile();
    }

    public boolean isEmptyDirectory() {
        return this.emptyDirectory;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        StandardConfigDataResource other = (StandardConfigDataResource) obj;
        return this.resource.equals(other.resource) && this.emptyDirectory == other.emptyDirectory;
    }

    @Override
    public int hashCode() {
        return this.resource.hashCode();
    }

    @Override
    public String toString() {
        if (this.resource instanceof FileSystemResource || this.resource instanceof FileUrlResource) {
            try {
                return "file [" + this.resource.getFile().toString() + "]";
            } catch (IOException ex) {
            }
        }
        return this.resource.toString();
    }

}
